package com.agritech.adapter;

/**
 * 高级无人机类
 * 第三方无人机接口的具体实现
 */
public class AdvancedDrone implements ThirdPartyDrone {
    private String droneModel;
    private boolean isPoweredOn;
    private boolean isFlying;
    private double currentAltitude;

    public AdvancedDrone(String model) {
        this.droneModel = model;
        this.isPoweredOn = false;
        this.isFlying = false;
        this.currentAltitude = 0.0;
        System.out.println("高级无人机 " + model + " 已初始化");
    }

    @Override
    public void powerOn() {
        if (!isPoweredOn) {
            isPoweredOn = true;
            System.out.println("无人机 " + droneModel + " 引擎已启动");
        } else {
            System.out.println("无人机 " + droneModel + " 引擎已经是启动状态");
        }
    }

    @Override
    public void powerOff() {
        if (isPoweredOn) {
            if (isFlying) {
                System.out.println("警告: 无人机 " + droneModel + " 正在飞行中，无法关闭引擎，请先降落");
                return;
            }
            isPoweredOn = false;
            System.out.println("无人机 " + droneModel + " 引擎已关闭");
        } else {
            System.out.println("无人机 " + droneModel + " 引擎已经是关闭状态");
        }
    }

    @Override
    public void takeOff(double altitude) {
        if (!isPoweredOn) {
            System.out.println("无人机 " + droneModel + " 引擎未启动，无法起飞");
            return;
        }
        if (isFlying) {
            System.out.println("无人机 " + droneModel + " 已经在飞行中");
            return;
        }
        isFlying = true;
        currentAltitude = altitude;
        System.out.println("无人机 " + droneModel + " 已起飞，当前高度: " + altitude + "米");
    }

    @Override
    public void land() {
        if (!isFlying) {
            System.out.println("无人机 " + droneModel + " 未在飞行状态，无需降落");
            return;
        }
        isFlying = false;
        currentAltitude = 0.0;
        System.out.println("无人机 " + droneModel + " 已降落");
    }

    @Override
    public void flyMission(String missionPlan) {
        if (!isFlying) {
            System.out.println("无人机 " + droneModel + " 未起飞，无法执行飞行任务");
            return;
        }
        System.out.println("无人机 " + droneModel + " 正在执行任务: " + missionPlan);
        // 模拟任务执行
        try {
            Thread.sleep(2000); // 模拟任务耗时
        } catch (InterruptedException e) {
            System.err.println("无人机任务被中断: " + e.getMessage());
            Thread.currentThread().interrupt();
        }
        System.out.println("无人机 " + droneModel + " 任务 '" + missionPlan + "' 执行完毕");
    }

    public boolean isFlying() {
        return isFlying;
    }

    public String getDroneStatus() {
        return "无人机型号: " + droneModel + 
               ", 电源: " + (isPoweredOn ? "开启" : "关闭") + 
               ", 飞行状态: " + (isFlying ? "飞行中" : "地面") + 
               ", 高度: " + currentAltitude + "米";
    }
}